package com.bawei.mall.user.controller;

import com.bawei.common.core.domain.R;
import com.bawei.mall.user.domain.*;
import com.bawei.mall.user.domain.vo.AddMiddleOrderRequest;
import com.bawei.mall.user.service.ConfirService;
import com.github.pagehelper.PageInfo;
import io.seata.core.model.Result;
import io.swagger.models.auth.In;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class ConfirmOrderController {
    @Autowired
    private ConfirService service;

    @GetMapping("/generateConfirmOrder")
    public R<ConfirmOrder> findgenerateConfirmOrder(@RequestParam List<String> ids){
        R<ConfirmOrder> confirmOrder = service.generateConfirmOrder(ids);
        return confirmOrder;
    }

    @PostMapping("generateOrder")
    public R addgenerateOrder(@RequestBody CusOrder cusOrder){
        service.generateOrder(cusOrder);
        return R.ok();
    }

    @PostMapping("AddMiddleOrder")
    public R<CusOrderInfo> AddMiddleOrder(@RequestBody AddMiddleOrderRequest request) {
        ArrayList<CusOrderInfo> cusOrderInfo = new ArrayList<>();
        CusOrderInfo cusOrderInfo1 = new CusOrderInfo();
        List<MallProductCar> mallProductCars = request.getMallProductCars();
        Integer orderId = request.getOrderId();
        for (MallProductCar mallProductCar : mallProductCars) {
            cusOrderInfo1.setSkuId(mallProductCar.getProductSkuId());
            cusOrderInfo1.setQuantity(mallProductCar.getQuantity());
            cusOrderInfo1.setPrice(mallProductCar.getPrice());
            cusOrderInfo1.setPic(mallProductCar.getProductPic());
            cusOrderInfo.add(cusOrderInfo1);
        }
        service.AddMiddleOrder(orderId,mallProductCars);
        return R.ok();
    }

    /**
     * 查询列表
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R<PageInfo<MallOrderList>> list(@RequestParam Integer status,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize ){
        PageInfo<MallOrderList> list =service.list(status,pageNum,pageSize);
        return R.ok(list);
    }

}
